package com.ruoyi.system.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.KsAttachment;
import com.ruoyi.system.domain.KsReplyLog;
import com.ruoyi.system.mapper.RxOrderFollowMapper;
import com.ruoyi.system.mapper.RxOrderMapper;
import com.ruoyi.system.service.IKsReplyLogService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 客诉回复Controller
 *
 * @author hjf
 * @date 2025-07-10
 */
@RestController
@RequestMapping("/hmk/ksReplylog")
public class KsReplyLogController extends BaseController {
    @Autowired
    private IKsReplyLogService ksReplyLogService;

    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private RxOrderMapper oM;
    @Autowired
    private RxOrderFollowMapper oFM;

    /**
     * 查询客诉回复列表
     */
    @PreAuthorize("@ss.hasPermi('hmk:ksReplylog:list')")
    @GetMapping("/list")
    public TableDataInfo list(KsReplyLog ksReplyLog) {
        List<KsReplyLog> list = ksReplyLogService.selectKsReplyLogList(ksReplyLog);
        for (KsReplyLog item : list) {
            List<KsAttachment> iamges = item.getKsAttachmentList().stream().filter(i -> "1".equals(i.getFileType())).collect(Collectors.toList());
            if (iamges.size() > 0) {
                item.setImageKsAttachmentList(iamges);
            }
            List<KsAttachment> audios = item.getKsAttachmentList().stream().filter(i -> "2".equals(i.getFileType())).collect(Collectors.toList());
            if (audios.size() > 0) {
                item.setAudioKsAttachmentList(audios);
            }
            if (null != item.getReferenceId()) {

            }
        }
        return getDataTable(list);
    }

    /**
     * 获取客诉回复详细信息
     */
    @PreAuthorize("@ss.hasPermi('hmk:ksReplylog:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(ksReplyLogService.selectKsReplyLogById(id));
    }

    /**
     * 新增客诉回复
     */
    @PreAuthorize("@ss.hasPermi('hmk:ksReplylog:add')")
    //@Log(title = "客诉回复", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody KsReplyLog ksReplyLog) {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        if (sysUser == null) {
            throw new ServiceException("用户未登录或会话已过期", 401);
        }
        if (StringUtils.isEmpty(sysUser.getNickName())) {
            sysUser = sysUserService.selectUserById(sysUser.getUserId());
        }
        ksReplyLog.setReplyUserId(sysUser.getUserId());
        return toAjax(ksReplyLogService.insertKsReplyLog(ksReplyLog));
    }


    /**
     * 修改客诉回复
     */
    @PreAuthorize("@ss.hasPermi('hmk:ksReplylog:edit')")
    //@Log(title = "客诉回复", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody KsReplyLog ksReplyLog) {
        return toAjax(ksReplyLogService.updateKsReplyLog(ksReplyLog));
    }

    /**
     * 删除客诉回复
     */
    @PreAuthorize("@ss.hasPermi('hmk:ksReplylog:remove')")
    //@Log(title = "客诉回复", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(ksReplyLogService.deleteKsReplyLogByIds(ids));
    }


}
